home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / bfile / bfile.cpp next >
Text File  |  1991-09-06  |  5KB  |  223 lines

  1. /*
  2.  
  3.       bfile.cpp
  4.       Btrieve class for Borland C++
  5.       09/06/91
  6.  
  7.       Douglas J. Reilly
  8.       Access Microsystems Inc.
  9.       404 Midstreams Road
  10.       Brick, New Jersey  08724
  11.       (908) 892-2683
  12.       CompuServe 74040,607
  13.  
  14.       Comments?  Questions?  Suggestions?
  15.       Have a paying C/C++ programming job you need done?
  16.       Give me a call.
  17.  
  18.       Released into the public domain.  Do with it as you see fit, but
  19.       if you do anything really neat with it, let me know...
  20.  
  21. */
  22. #include "stdio.h"
  23. #include "stdlib.h"
  24. #include "mem.h"
  25. #include "string.h"
  26. #include "bfile.h"    // class description for bfile
  27.  
  28. class bfile;
  29.  
  30. /*#define TEST*/
  31. #ifdef TEST
  32. main()
  33. {
  34.    char temp[100];
  35.    int  ret;
  36.    temp[0]='\0';
  37.    bfile sum("TEST.BTR",4096,"BOOO!",0);
  38.  
  39.  
  40.    printf("\nsum.get_rec(temp,B_GET_LO)=%d data=%30.30s",
  41.                                            sum.get_rec(temp,B_GET_LO),
  42.                                            sum.get_data());
  43.    do {
  44.       printf("\nsum.get_rec(temp,B_GET_NEXT)=%d data=%30.30s",
  45.                                            ret=sum.get_rec(temp,B_GET_NEXT),
  46.                                            sum.get_data());
  47.    } while ( ret==0 );
  48. }
  49. #endif
  50.  
  51. bfile::bfile(char *name,int len,char *towner,int newmode)
  52. {
  53.    rec_len=len;
  54.    data=new char[rec_len];
  55.  
  56.    key_num=0;
  57.    opened=0;
  58.    strcpy(fname,name);
  59.    mode=newmode;
  60.    if ( data==0 )
  61.    {
  62.       printf("\nOpen failed for %s-No memory",fname);
  63.    }
  64.    else
  65.    {
  66.       if ( towner!=0 )
  67.       {
  68.          strcpy(data,towner);
  69.          strcpy(owner,towner);
  70.       }
  71.       status=BTRV(B_OPEN,pos_blk,data,&rec_len,fname,mode);
  72.       if ( status )
  73.       {
  74.          delete data;
  75.          data=0;
  76.          printf("\nOpen failed for %s-status %d",fname,status);
  77.       }
  78.       else
  79.       {
  80.          opened=1;
  81.       }
  82.    }
  83. }
  84. bfile::~bfile()
  85. {
  86.    if ( !(opened) )
  87.    {
  88.       return;
  89.    }
  90.    else
  91.    {
  92.       status=BTRV(B_CLOSE,pos_blk,data,&rec_len,fname,mode);
  93.       delete data;
  94.       data=0;
  95.       opened=0;
  96.    }
  97. }
  98.  
  99. int bfile::close()
  100. {
  101.    if ( !(opened) )
  102.    {
  103.       return 0;
  104.    }
  105.    else
  106.    {
  107.       status=BTRV(B_CLOSE,pos_blk,data,&rec_len,fname,mode);
  108.       delete data;
  109.       data=0;
  110.       opened=0;
  111.       return 1;
  112.    }
  113. }
  114.  
  115. int bfile::open(int newmode)
  116. {
  117.    if ( opened )
  118.    {
  119.       return 0;
  120.    }
  121.    if ( newmode!=-1 )
  122.    {
  123.       mode=newmode;
  124.    }
  125.    opened=0;
  126.    data=new char[rec_len];
  127.    if ( data==0 )
  128.    {
  129.       printf("\nOpen failed for %s-No memory",fname);
  130.    }
  131.    if ( owner!=0 )
  132.    {
  133.       strcpy(data,owner);
  134.    }
  135.    status=BTRV(B_OPEN,pos_blk,data,&rec_len,fname,mode);
  136.    if ( status )
  137.    {
  138.       delete data;
  139.       data=0;
  140.       printf("\nOpen failed for %s-status %d",fname,status);
  141.    }
  142.    else
  143.    {
  144.       opened=1;
  145.    }
  146. }
  147.  
  148. int bfile::get_rec(char *keystr,int op)
  149. {
  150.    if ( !(opened) || data==0 )
  151.    {
  152.       return(BERR_NO_OPEN);    // btrieve status for not opened...
  153.    }
  154.    status=BTRV(op,pos_blk,data,&rec_len,keystr,key_num);
  155.    return(status);
  156. }
  157.  
  158. int bfile::put_rec(char *keystr,int )
  159. {
  160.    char *tdata;
  161.    char tkey[128];
  162.    int  stat;
  163.  
  164.    tdata=new char[rec_len];
  165.    memcpy(tdata,data,rec_len);
  166.    memcpy(tkey,keystr,128);
  167.   // This is how I save btrieve data.  I understand that there is some
  168.   //   potential for problem with this, since by always re-establishing
  169.   //   positioning, I won't know if someone changed the data since I last
  170.   //   got the rec, but in things I have done, I lock the records first
  171.   //   anyway...
  172.    if ( (stat=get_rec(keystr))!=0 )
  173.    {
  174.       if ( stat!=BERR_REC_NOT_FOUND && stat!=BERR_EOF )
  175.       {
  176.          delete tdata;
  177.          return(stat);
  178.       }
  179.      // new record?
  180.       status=BTRV(B_INSERT,pos_blk,tdata,&rec_len,tkey,key_num);
  181.    }
  182.    else
  183.    {
  184.       status=BTRV(B_UPDATE,pos_blk,tdata,&rec_len,tkey,key_num);
  185.    }
  186.    if ( !(status) )
  187.    {
  188.       memcpy(data,tdata,rec_len);
  189.       strcpy(keystr,tkey);
  190.    }
  191.    delete tdata;
  192.    return status;
  193. }
  194.  
  195. int bfile::del_rec(char *keystr)
  196. {
  197.    char *tdata;
  198.    char tkey[128];
  199.    int  stat;
  200.  
  201.   // use tdata so as not to destroy infor in data...
  202.    tdata=new char[rec_len];
  203.    memcpy(tdata,data,rec_len);
  204.    memcpy(tkey,keystr,128);
  205.    if ( (stat=get_rec(keystr))!=0 )
  206.    {
  207.       delete tdata;
  208.       return(stat);
  209.    }
  210.    else
  211.    {
  212.       status=BTRV(B_DELETE,pos_blk,tdata,&rec_len,tkey,key_num);
  213.    }
  214.    if ( !(status) )
  215.    {
  216.       memcpy(data,tdata,rec_len);
  217.       strcpy(keystr,tkey);
  218.    }
  219.    delete tdata;
  220.    return status;
  221. }
  222.  
  223.